/*
 * mic.c
 *
 * realize mic event.
 *
 * Copyright (C) 2024 Sinh Micro, Inc.
 * Subject to the GNU Public License, version 2.
 *
 * Author: lixiang<lixiang@sinhmicro.com>
 * 
 * Encoding format: GB2312
 * Version: v1.2.2
 * Date: 2024-11-05
 */

#include "hal/inc/hal-regs.h"
#include "hal/inc/types.h"
#include "hal/inc/gpio.h"

#include "sys_event.h"
#include "mic.h"
#include "power_manage.h"
#include "mic_det_api.h"
#include "heat.h"
#include "debug.h"
#include "ec_app_config.h"
#include "heater_plugging_detect.h"

#define MIC_PRESS_DEBOUNCE_CNT        (30)  

#if (CONFIG_MIC_TYPE == 0)
bool mic_is_trigger(void)
{
    uint8_t port = CONFIG_MIC_IO_SEL >> 4;
    uint8_t num = CONFIG_MIC_IO_SEL & 0x0f;
    uint8_t io_status;

    if (port == 0) {
        io_status = P0IDAT & (1 << num);
    } else {
        io_status = P1IDAT & (1 << num);
    }

#if (CONFIG_MIC_ACTIVE_LOW == 1)
    return io_status ? 0 : 1;
#else
    return io_status ? 1 : 0;
#endif
}

void mic_init(void)
{
#if (CONFIG_MIC_IO_SEL == 3)
    GPIO_MFP_P03(MFP_P03_P03); 
    GPIO_P0_IM_EN(0x08);
#if (CONFIG_MIC_ACTIVE_LOW == 1)
    GPIO_P0_PU_EN(0x08);
#else
    GPIO_P0_PD_EN(0x08);
#endif
#elif (CONFIG_MIC_IO_SEL == 7)
    GPIO_MFP_P07(MFP_P07_P07); 
    GPIO_P0_IM_EN(0x80);
#if (CONFIG_MIC_ACTIVE_LOW == 1)
    GPIO_P0_PU_EN(0x80);
#else
    GPIO_P0_PD_EN(0x80);
#endif
#elif (CONFIG_MIC_IO_SEL == 16)
    GPIO_MFP_P10(MFP_P10_P10); 
    GPIO_P1_0_4_IM_EN(0x01);
#if (CONFIG_MIC_ACTIVE_LOW == 1)
    GPIO_P1_0_3_PU_EN(0x01);
#else
    GPIO_P1_0_3_PD_EN(0x01);
#endif
#elif (CONFIG_MIC_IO_SEL == 17)
    GPIO_MFP_P11(MFP_P11_P11); 
    GPIO_P1_0_4_IM_EN(0x02);
#if (CONFIG_MIC_ACTIVE_LOW == 1)
    GPIO_P1_0_3_PU_EN(0x02);
#else
    GPIO_P1_0_3_PD_EN(0x02);
#endif
#elif (CONFIG_MIC_IO_SEL == 18)
    GPIO_MFP_P12(MFP_P12_P12); 
    GPIO_P1_0_4_IM_EN(0x04);
#if (CONFIG_MIC_ACTIVE_LOW == 1)
    GPIO_P1_0_3_PU_EN(0x04);
#else
    GPIO_P1_0_3_PD_EN(0x04);
#endif
#endif
}
#endif

void mic_handle_10ms(void)
{
#if (CONFIG_PLUGE_DETECT == 1)    
    static idata uint8_t mic_press_cnt;
#endif
    static bool mic_last_flag;
    bool mic_curr_flag;

#if (CONFIG_MIC_TYPE == 0)
    mic_curr_flag = mic_is_trigger();
#else
    mic_curr_flag = mic_det_is_trigger();
#endif

#if (CONFIG_PLUGE_DETECT == 1)
    if (mic_curr_flag != mic_last_flag) {
        if (mic_curr_flag == true) {
            mic_press_cnt = (heater_is_in()) ? MIC_PRESS_DEBOUNCE_CNT : 1;
        } else {
            mic_press_cnt = 0;
            event_state_mask |= EVENT_STATE_MIC_RELEASED;
        }
    }

    if ((mic_press_cnt >= 1) && (mic_press_cnt < MIC_PRESS_DEBOUNCE_CNT)) {
        mic_press_cnt++;
    }

    if (mic_press_cnt == MIC_PRESS_DEBOUNCE_CNT) {
        mic_press_cnt = 0;
        event_state_mask |= EVENT_STATE_MIC_PRESSED;
        pm_sleep_cnt_reset();
    }
#else
    if (mic_curr_flag != mic_last_flag) {
        if (mic_curr_flag == true) {
            event_state_mask |= EVENT_STATE_MIC_PRESSED;
            debug_print("mic-p\n", 0);
        } else {
            event_state_mask |= EVENT_STATE_MIC_RELEASED;
        }
    }    
#endif

    mic_last_flag = mic_curr_flag;    
}
